home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / ld / ldmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-26  |  23.5 KB  |  924 lines

  1. /* Main program of GNU linker.
  2.    Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
  3.    Written by Steve Chamberlain steve@cygnus.com
  4.  
  5. This file is part of GLD, the Gnu Linker.
  6.  
  7. GLD is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GLD is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GLD; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include <stdio.h>
  25. #include "libiberty.h"
  26. #include "bfdlink.h"
  27.  
  28. #include "config.h"
  29. #include "ld.h"
  30. #include "ldmain.h"
  31. #include "ldmisc.h"
  32. #include "ldwrite.h"
  33. #include "ldgram.h"
  34. #include "ldexp.h"
  35. #include "ldlang.h"
  36. #include "ldemul.h"
  37. #include "ldlex.h"
  38. #include "ldfile.h"
  39. #include "ldctor.h"
  40.  
  41. /* Somewhere above, sys/stat.h got included . . . . */
  42. #if !defined(S_ISDIR) && defined(S_IFDIR)
  43. #define    S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  44. #endif
  45.  
  46. #include <string.h>
  47.  
  48. static char *get_emulation PARAMS ((int, char **));
  49. static void set_scripts_dir PARAMS ((void));
  50.  
  51. /* EXPORTS */
  52.  
  53. char *default_target;
  54. const char *output_filename = "a.out";
  55.  
  56. /* Name this program was invoked by.  */
  57. char *program_name;
  58.  
  59. /* The file that we're creating */
  60. bfd *output_bfd = 0;
  61.  
  62. /* Set by -G argument, for MIPS ECOFF target.  */
  63. int g_switch_value = 8;
  64.  
  65. /* Nonzero means print names of input files as processed.  */
  66. boolean trace_files;
  67.  
  68. /* Nonzero means same, but note open failures, too.  */
  69. boolean trace_file_tries;
  70.  
  71. /* Nonzero means version number was printed, so exit successfully
  72.    instead of complaining if no input files are given.  */
  73. boolean version_printed;
  74.  
  75. args_type command_line;
  76.  
  77. ld_config_type config;
  78.  
  79. static boolean check_for_scripts_dir PARAMS ((char *dir));
  80. static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
  81.                         const char *));
  82. static boolean multiple_definition PARAMS ((struct bfd_link_info *,
  83.                         const char *,
  84.                         bfd *, asection *, bfd_vma,
  85.                         bfd *, asection *, bfd_vma));
  86. static boolean multiple_common PARAMS ((struct bfd_link_info *,
  87.                     const char *, bfd *,
  88.                     enum bfd_link_hash_type, bfd_vma,
  89.                     bfd *, enum bfd_link_hash_type,
  90.                     bfd_vma));
  91. static boolean add_to_set PARAMS ((struct bfd_link_info *,
  92.                    struct bfd_link_hash_entry *,
  93.                    bfd_reloc_code_real_type,
  94.                    bfd *, asection *, bfd_vma));
  95. static boolean constructor_callback PARAMS ((struct bfd_link_info *,
  96.                          boolean constructor,
  97.                          const char *name,
  98.                          bfd *, asection *, bfd_vma));
  99. static boolean warning_callback PARAMS ((struct bfd_link_info *,
  100.                      const char *));
  101. static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
  102.                      const char *, bfd *,
  103.                      asection *, bfd_vma));
  104. static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
  105.                        const char *, bfd_vma,
  106.                        bfd *, asection *, bfd_vma));
  107. static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
  108.                     bfd *, asection *, bfd_vma));
  109. static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
  110.                      const char *, bfd *, asection *,
  111.                      bfd_vma));
  112. static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
  113.                     bfd *, asection *, bfd_vma));
  114.  
  115. static struct bfd_link_callbacks link_callbacks =
  116. {
  117.   add_archive_element,
  118.   multiple_definition,
  119.   multiple_common,
  120.   add_to_set,
  121.   constructor_callback,
  122.   warning_callback,
  123.   undefined_symbol,
  124.   reloc_overflow,
  125.   reloc_dangerous,
  126.   unattached_reloc,
  127.   notice_ysym
  128. };
  129.  
  130. struct bfd_link_info link_info;
  131.  
  132. static void
  133. remove_output ()
  134. {
  135.   if (output_filename) 
  136.     {
  137.       if (output_bfd && output_bfd->iostream)
  138.     fclose((FILE *)(output_bfd->iostream));
  139.       if (delete_output_file_on_failure)
  140.     unlink (output_filename);
  141.     }
  142. }
  143.  
  144. int
  145. main (argc, argv)
  146.      int argc;
  147.      char **argv;
  148. {
  149.   char *emulation;
  150.   long start_time = get_run_time ();
  151.  
  152.   program_name = argv[0];
  153.   xmalloc_set_program_name (program_name);
  154.  
  155.   bfd_init ();
  156.  
  157.   xatexit (remove_output);
  158.  
  159.   /* Initialize the data about options.  */
  160.   trace_files = trace_file_tries = version_printed = false;
  161.   config.traditional_format = false;
  162.   config.build_constructors = true;
  163.   config.dynamic_link = false;
  164.   command_line.force_common_definition = false;
  165.   command_line.interpreter = NULL;
  166.   command_line.rpath = NULL;
  167.  
  168.   link_info.callbacks = &link_callbacks;
  169.   link_info.relocateable = false;
  170.   link_info.shared = false;
  171.   link_info.strip = strip_none;
  172.   link_info.discard = discard_none;
  173.   link_info.lprefix_len = 1;
  174.   link_info.lprefix = "L";
  175.   link_info.keep_memory = true;
  176.   link_info.input_bfds = NULL;
  177.   link_info.create_object_symbols_section = NULL;
  178.   link_info.hash = NULL;
  179.   link_info.keep_hash = NULL;
  180.   link_info.notice_hash = NULL;
  181.  
  182.   ldfile_add_arch ("");
  183.  
  184.   config.make_executable = true;
  185.   force_make_executable = false;
  186.   config.magic_demand_paged = true;
  187.   config.text_read_only = true;
  188.   config.make_executable = true;
  189.  
  190.   emulation = get_emulation (argc, argv);
  191.   ldemul_choose_mode (emulation);
  192.   default_target = ldemul_choose_target ();
  193.   lang_init ();
  194.   ldemul_before_parse ();
  195.   lang_has_input_file = false;
  196.   parse_args (argc, argv);
  197.  
  198.   if (link_info.relocateable)
  199.     {
  200.       if (command_line.relax)
  201.     einfo ("%P%F: -relax and -r may not be used together\n");
  202.       if (config.dynamic_link)
  203.     einfo ("%P%F: -r and -call_shared may not be used together\n");
  204.       if (link_info.shared)
  205.     einfo ("%P%F: -r and -shared may not be used together\n");
  206.     }
  207.  
  208.   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
  209.      don't see how else this can be handled, since in this case we
  210.      must preserve all externally visible symbols.  */
  211.   if (link_info.relocateable && link_info.strip == strip_all)
  212.     {
  213.       link_info.strip = strip_debugger;
  214.       if (link_info.discard == discard_none)
  215.     link_info.discard = discard_all;
  216.     }
  217.  
  218.   /* This essentially adds another -L directory so this must be done after
  219.      the -L's in argv have been processed.  */
  220.   set_scripts_dir ();
  221.  
  222.   if (had_script == false)
  223.     {
  224.       /* Read the emulation's appropriate default script.  */
  225.       int isfile;
  226.       char *s = ldemul_get_script (&isfile);
  227.  
  228.       if (isfile)
  229.     ldfile_open_command_file (s);
  230.       else
  231.     lex_redirect (s);
  232.       parser_input = input_script;
  233.       yyparse ();
  234.     }
  235.  
  236.   lang_final ();
  237.  
  238.   if (lang_has_input_file == false)
  239.     {
  240.       if (version_printed)
  241.     xexit (0);
  242.       einfo ("%P%F: no input files\n");
  243.     }
  244.  
  245.   if (trace_files)
  246.     {
  247.       info_msg ("%P: mode %s\n", emulation);
  248.     }
  249.  
  250.   ldemul_after_parse ();
  251.  
  252.  
  253.   if (config.map_filename)
  254.     {
  255.       if (strcmp (config.map_filename, "-") == 0)
  256.     {
  257.       config.map_file = stdout;
  258.     }
  259.       else
  260.     {
  261.       config.map_file = fopen (config.map_filename, FOPEN_WT);
  262.       if (config.map_file == (FILE *) NULL)
  263.         {
  264.           einfo ("%P%F: cannot open map file %s: %E\n",
  265.              config.map_filename);
  266.         }
  267.     }
  268.     }
  269.  
  270.  
  271.   lang_process ();
  272.  
  273.   /* Print error messages for any missing symbols, for any warning
  274.      symbols, and possibly multiple definitions */
  275.  
  276.  
  277.   if (config.text_read_only)
  278.     {
  279.       /* Look for a text section and mark the readonly attribute in it */
  280.       asection *found = bfd_get_section_by_name (output_bfd, ".text");
  281.  
  282.       if (found != (asection *) NULL)
  283.     {
  284.       found->flags |= SEC_READONLY;
  285.     }
  286.     }
  287.  
  288.   if (link_info.relocateable)
  289.     output_bfd->flags &= ~EXEC_P;
  290.   else
  291.     output_bfd->flags |= EXEC_P;
  292.  
  293.   ldwrite ();
  294.  
  295.   /* Even if we're producing relocateable output, some non-fatal errors should
  296.      be reported in the exit status.  (What non-fatal errors, if any, do we
  297.      want to ignore for relocateable output?)  */
  298.  
  299.   if (config.make_executable == false && force_make_executable == false)
  300.     {
  301.       if (trace_files == true)
  302.     {
  303.       einfo ("%P: link errors found, deleting executable `%s'\n",
  304.          output_filename);
  305.     }
  306.  
  307.       if (output_bfd->iostream)
  308.     fclose ((FILE *) (output_bfd->iostream));
  309.  
  310.       unlink (output_filename);
  311.       xexit (1);
  312.     }
  313.   else
  314.     {
  315.       if (! bfd_close (output_bfd))
  316.     einfo ("%F%B: final close failed: %E\n", output_bfd);
  317.     }
  318.  
  319.   if (config.stats)
  320.     {
  321.       extern char **environ;
  322.       char *lim = (char *) sbrk (0);
  323.       long run_time = get_run_time () - start_time;
  324.  
  325.       fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
  326.            program_name, run_time / 1000000, run_time % 1000000);
  327.       fprintf (stderr, "%s: data size %ld\n", program_name,
  328.            (long) (lim - (char *) &environ));
  329.     }
  330.  
  331.   /* Prevent remove_output from doing anything, after a successful link.  */
  332.   output_filename = NULL;
  333.  
  334.   xexit (0);
  335.   return 0;
  336. }
  337.  
  338. /* We need to find any explicitly given emulation in order to initialize the
  339.    state that's needed by the lex&yacc argument parser (parse_args).  */
  340.  
  341. static char *
  342. get_emulation (argc, argv)
  343.      int argc;
  344.      char **argv;
  345. {
  346.   char *emulation;
  347.   int i;
  348.  
  349.   emulation = (char *) getenv (EMULATION_ENVIRON);
  350.   if (emulation == NULL)
  351.     emulation = DEFAULT_EMULATION;
  352.  
  353.   for (i = 1; i < argc; i++)
  354.     {
  355.       if (!strncmp (argv[i], "-m", 2))
  356.     {
  357.       if (argv[i][2] == '\0')
  358.         {
  359.           /* -m EMUL */
  360.           if (i < argc - 1)
  361.         {
  362.           emulation = argv[i + 1];
  363.           i++;
  364.         }
  365.           else
  366.         {
  367.           einfo("%P%F: missing argument to -m\n");
  368.         }
  369.         }
  370.       else if (strcmp (argv[i], "-mips1") == 0
  371.            || strcmp (argv[i], "-mips2") == 0
  372.            || strcmp (argv[i], "-mips3") == 0)
  373.         {
  374.           /* FIXME: The arguments -mips1, -mips2 and -mips3 are
  375.          passed to the linker by some MIPS compilers.  They
  376.          generally tell the linker to use a slightly different
  377.          library path.  Perhaps someday these should be
  378.          implemented as emulations; until then, we just ignore
  379.          the arguments and hope that nobody ever creates
  380.          emulations named ips1, ips2 or ips3.  */
  381.         }
  382.       else if (strcmp (argv[i], "-m486") == 0)
  383.         {
  384.           /* FIXME: The argument -m486 is passed to the linker on
  385.          some Linux systems.  Hope that nobody creates an
  386.          emulation named 486.  */
  387.         }
  388.       else
  389.         {
  390.           /* -mEMUL */
  391.           emulation = &argv[i][2];
  392.         }
  393.     }
  394.     }
  395.  
  396.   return emulation;
  397. }
  398.  
  399. /* If directory DIR contains an "ldscripts" subdirectory,
  400.    add DIR to the library search path and return true,
  401.    else return false.  */
  402.  
  403. static boolean
  404. check_for_scripts_dir (dir)
  405.      char *dir;
  406. {
  407.   size_t dirlen;
  408.   char *buf;
  409.   struct stat s;
  410.   boolean res;
  411.  
  412.   dirlen = strlen (dir);
  413.   /* sizeof counts the terminating NUL.  */
  414.   buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
  415.   sprintf (buf, "%s/ldscripts", dir);
  416.  
  417.   res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
  418.   free (buf);
  419.   if (res)
  420.     ldfile_add_library_path (dir, false);
  421.   return res;
  422. }
  423.  
  424. /* Set the default directory for finding script files.
  425.    Libraries will be searched for here too, but that's ok.
  426.    We look for the "ldscripts" directory in:
  427.  
  428.    SCRIPTDIR (passed from Makefile)
  429.    the dir where this program is (for using it from the build tree)
  430.    the dir where this program is/../lib (for installing the tool suite elsewhere) */
  431.  
  432. static void
  433. set_scripts_dir ()
  434. {
  435.   char *end, *dir;
  436.   size_t dirlen;
  437.  
  438.   if (check_for_scripts_dir (SCRIPTDIR))
  439.     return;            /* We've been installed normally.  */
  440.  
  441.   /* Look for "ldscripts" in the dir where our binary is.  */
  442.   end = strrchr (program_name, '/');
  443.   if (end)
  444.     {
  445.       dirlen = end - program_name;
  446.       /* Make a copy of program_name in dir.
  447.      Leave room for later "/../lib".  */
  448.       dir = (char *) xmalloc (dirlen + 8);
  449.       strncpy (dir, program_name, dirlen);
  450.       dir[dirlen] = '\0';
  451.     }
  452.   else
  453.     {
  454.       dirlen = 1;
  455.       dir = (char *) xmalloc (dirlen + 8);
  456.       strcpy (dir, ".");
  457.     }
  458.  
  459.   if (check_for_scripts_dir (dir))
  460.     return;            /* Don't free dir.  */
  461.  
  462.   /* Look for "ldscripts" in <the dir where our binary is>/../lib.  */
  463.   strcpy (dir + dirlen, "/../lib");
  464.   if (check_for_scripts_dir (dir))
  465.     return;
  466.  
  467.   free (dir);            /* Well, we tried.  */
  468. }
  469.  
  470. void
  471. add_ysym (name)
  472.      const char *name;
  473. {
  474.   if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
  475.     {
  476.       link_info.notice_hash = ((struct bfd_hash_table *)
  477.                    xmalloc (sizeof (struct bfd_hash_table)));
  478.       if (! bfd_hash_table_init_n (link_info.notice_hash,
  479.                    bfd_hash_newfunc,
  480.                    61))
  481.     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
  482.     }      
  483.  
  484.   if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
  485.       == (struct bfd_hash_entry *) NULL)
  486.     einfo ("%P%F: bfd_hash_lookup failed: %E\n");
  487. }
  488.  
  489. /* Handle the -retain-symbols-file option.  */
  490.  
  491. void
  492. add_keepsyms_file (filename)
  493.      const char *filename;
  494. {
  495.   FILE *file;
  496.   char *buf;
  497.   size_t bufsize;
  498.   int c;
  499.  
  500.   if (link_info.strip == strip_some)
  501.     einfo ("%X%P: error: duplicate retain-symbols-file\n");
  502.  
  503.   file = fopen (filename, "r");
  504.   if (file == (FILE *) NULL)
  505.     {
  506.       bfd_set_error (bfd_error_system_call);
  507.       einfo ("%X%P: %s: %E", filename);
  508.       return;
  509.     }
  510.  
  511.   link_info.keep_hash = ((struct bfd_hash_table *)
  512.              xmalloc (sizeof (struct bfd_hash_table)));
  513.   if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
  514.     einfo ("%P%F: bfd_hash_table_init failed: %E\n");
  515.  
  516.   bufsize = 100;
  517.   buf = (char *) xmalloc (bufsize);
  518.  
  519.   c = getc (file);
  520.   while (c != EOF)
  521.     {
  522.       while (isspace (c))
  523.     c = getc (file);
  524.  
  525.       if (c != EOF)
  526.     {
  527.       size_t len = 0;
  528.  
  529.       while (! isspace (c) && c != EOF)
  530.         {
  531.           buf[len] = c;
  532.           ++len;
  533.           if (len >= bufsize)
  534.         {
  535.           bufsize *= 2;
  536.           buf = xrealloc (buf, bufsize);
  537.         }
  538.           c = getc (file);
  539.         }
  540.  
  541.       buf[len] = '\0';
  542.  
  543.       if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
  544.           == (struct bfd_hash_entry *) NULL)
  545.         einfo ("%P%F: bfd_hash_lookup for insertion failed: %E");
  546.     }
  547.     }
  548.  
  549.   if (link_info.strip != strip_none)
  550.     einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
  551.  
  552.   link_info.strip = strip_some;
  553. }
  554.  
  555. /* Callbacks from the BFD linker routines.  */
  556.  
  557. /* This is called when BFD has decided to include an archive member in
  558.    a link.  */
  559.  
  560. /*ARGSUSED*/
  561. static boolean
  562. add_archive_element (info, abfd, name)
  563.      struct bfd_link_info *info;
  564.      bfd *abfd;
  565.      const char *name;
  566. {
  567.   lang_input_statement_type *input;
  568.  
  569.   input = ((lang_input_statement_type *)
  570.        xmalloc ((bfd_size_type) sizeof (lang_input_statement_type)));
  571.   input->filename = abfd->filename;
  572.   input->local_sym_name = abfd->filename;
  573.   input->the_bfd = abfd;
  574.   input->asymbols = NULL;
  575.   input->next = NULL;
  576.   input->just_syms_flag = false;
  577.   input->loaded = false;
  578.   input->search_dirs_flag = false;
  579.  
  580.   /* FIXME: The following fields are not set: header.next,
  581.      header.type, closed, passive_position, symbol_count,
  582.      next_real_file, is_archive, target, real, common_section,
  583.      common_output_section, complained.  This bit of code is from the
  584.      old decode_library_subfile function.  I don't know whether any of
  585.      those fields matters.  */
  586.  
  587.   ldlang_add_file (input);
  588.  
  589.   if (config.map_file != (FILE *) NULL)
  590.     minfo ("%s needed due to %T\n", abfd->filename, name);
  591.  
  592.   if (trace_files || trace_file_tries)
  593.     info_msg ("%I\n", input);
  594.  
  595.   return true;
  596. }
  597.  
  598. /* This is called when BFD has discovered a symbol which is defined
  599.    multiple times.  */
  600.  
  601. /*ARGSUSED*/
  602. static boolean
  603. multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
  604.      struct bfd_link_info *info;
  605.      const char *name;
  606.      bfd *obfd;
  607.      asection *osec;
  608.      bfd_vma oval;
  609.      bfd *nbfd;
  610.      asection *nsec;
  611.      bfd_vma nval;
  612. {
  613.   einfo ("%X%C: multiple definition of `%T'\n",
  614.      nbfd, nsec, nval, name);
  615.   if (obfd != (bfd *) NULL)
  616.     einfo ("%D: first defined here\n", obfd, osec, oval);
  617.   return true;
  618. }
  619.  
  620. /* This is called when there is a definition of a common symbol, or
  621.    when a common symbol is found for a symbol that is already defined,
  622.    or when two common symbols are found.  We only do something if
  623.    -warn-common was used.  */
  624.  
  625. /*ARGSUSED*/
  626. static boolean
  627. multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
  628.      struct bfd_link_info *info;
  629.      const char *name;
  630.      bfd *obfd;
  631.      enum bfd_link_hash_type otype;
  632.      bfd_vma osize;
  633.      bfd *nbfd;
  634.      enum bfd_link_hash_type ntype;
  635.      bfd_vma nsize;
  636. {
  637.   if (! config.warn_common)
  638.     return true;
  639.  
  640.   if (ntype == bfd_link_hash_defined
  641.       || ntype == bfd_link_hash_indirect)
  642.     {
  643.       ASSERT (otype == bfd_link_hash_common);
  644.       einfo ("%B: warning: definition of `%T' overriding common\n",
  645.          nbfd, name);
  646.       if (obfd != NULL)
  647.     einfo ("%B: warning: common is here\n", obfd);
  648.     }
  649.   else if (otype == bfd_link_hash_defined
  650.        || otype == bfd_link_hash_indirect)
  651.     {
  652.       ASSERT (ntype == bfd_link_hash_common);
  653.       einfo ("%B: warning: common of `%T' overridden by definition\n",
  654.          nbfd, name);
  655.       if (obfd != NULL)
  656.     einfo ("%B: warning: defined here\n", obfd);
  657.     }
  658.   else
  659.     {
  660.       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
  661.       if (osize > nsize)
  662.     {
  663.       einfo ("%B: warning: common of `%T' overridden by larger common\n",
  664.          nbfd, name);
  665.       if (obfd != NULL)
  666.         einfo ("%B: warning: larger common is here\n", obfd);
  667.     }
  668.       else if (nsize > osize)
  669.     {
  670.       einfo ("%B: warning: common of `%T' overriding smaller common\n",
  671.          nbfd, name);
  672.       if (obfd != NULL)
  673.         einfo ("%B: warning: smaller common is here\n", obfd);
  674.     }
  675.       else
  676.     {
  677.       einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
  678.       if (obfd != NULL)
  679.         einfo ("%B: warning: previous common is here\n", obfd);
  680.     }
  681.     }
  682.  
  683.   return true;
  684. }
  685.  
  686. /* This is called when BFD has discovered a set element.  H is the
  687.    entry in the linker hash table for the set.  SECTION and VALUE
  688.    represent a value which should be added to the set.  */
  689.  
  690. /*ARGSUSED*/
  691. static boolean
  692. add_to_set (info, h, reloc, abfd, section, value)
  693.      struct bfd_link_info *info;
  694.      struct bfd_link_hash_entry *h;
  695.      bfd_reloc_code_real_type reloc;
  696.      bfd *abfd;
  697.      asection *section;
  698.      bfd_vma value;
  699. {
  700.   if (! config.build_constructors)
  701.     return true;
  702.  
  703.   ldctor_add_set_entry (h, reloc, section, value);
  704.  
  705.   if (h->type == bfd_link_hash_new)
  706.     {
  707.       h->type = bfd_link_hash_undefined;
  708.       h->u.undef.abfd = abfd;
  709.       /* We don't call bfd_link_add_undef to add this to the list of
  710.      undefined symbols because we are going to define it
  711.      ourselves.  */
  712.     }
  713.  
  714.   return true;
  715. }
  716.  
  717. /* This is called when BFD has discovered a constructor.  This is only
  718.    called for some object file formats--those which do not handle
  719.    constructors in some more clever fashion.  This is similar to
  720.    adding an element to a set, but less general.  */
  721.  
  722. static boolean
  723. constructor_callback (info, constructor, name, abfd, section, value)
  724.      struct bfd_link_info *info;
  725.      boolean constructor;
  726.      const char *name;
  727.      bfd *abfd;
  728.      asection *section;
  729.      bfd_vma value;
  730. {
  731.   char *set_name;
  732.   char *s;
  733.   struct bfd_link_hash_entry *h;
  734.  
  735.   if (! config.build_constructors)
  736.     return true;
  737.  
  738.   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
  739.      useful error message.  */
  740.   if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL)
  741.     einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported");
  742.  
  743.   set_name = (char *) alloca (1 + sizeof "__CTOR_LIST__");
  744.   s = set_name;
  745.   if (bfd_get_symbol_leading_char (abfd) != '\0')
  746.     *s++ = bfd_get_symbol_leading_char (abfd);
  747.   if (constructor)
  748.     strcpy (s, "__CTOR_LIST__");
  749.   else
  750.     strcpy (s, "__DTOR_LIST__");
  751.  
  752.   if (config.map_file != (FILE *) NULL)
  753.     fprintf (config.map_file,
  754.          "Adding %s to constructor/destructor set %s\n", name, set_name);
  755.  
  756.   h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
  757.   if (h == (struct bfd_link_hash_entry *) NULL)
  758.     einfo ("%P%F: bfd_link_hash_lookup failed: %E");
  759.   if (h->type == bfd_link_hash_new)
  760.     {
  761.       h->type = bfd_link_hash_undefined;
  762.       h->u.undef.abfd = abfd;
  763.       /* We don't call bfd_link_add_undef to add this to the list of
  764.      undefined symbols because we are going to define it
  765.      ourselves.  */
  766.     }
  767.  
  768.   ldctor_add_set_entry (h, BFD_RELOC_CTOR, section, value);
  769.   return true;
  770. }
  771.  
  772. /* This is called when there is a reference to a warning symbol.  */
  773.  
  774. /*ARGSUSED*/
  775. static boolean
  776. warning_callback (info, warning)
  777.      struct bfd_link_info *info;
  778.      const char *warning;
  779. {
  780.   einfo ("%P: %s\n", warning);
  781.   return true;
  782. }
  783.  
  784. /* This is called when an undefined symbol is found.  */
  785.  
  786. /*ARGSUSED*/
  787. static boolean
  788. undefined_symbol (info, name, abfd, section, address)
  789.      struct bfd_link_info *info;
  790.      const char *name;
  791.      bfd *abfd;
  792.      asection *section;
  793.      bfd_vma address;
  794. {
  795.   static char *error_name;
  796.   static unsigned int error_count;
  797.  
  798. #define MAX_ERRORS_IN_A_ROW 5
  799.  
  800.   if (config.warn_once)
  801.     {
  802.       static struct bfd_hash_table *hash;
  803.  
  804.       /* Only warn once about a particular undefined symbol.  */
  805.  
  806.       if (hash == NULL)
  807.     {
  808.       hash = ((struct bfd_hash_table *)
  809.           xmalloc (sizeof (struct bfd_hash_table)));
  810.       if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
  811.         einfo ("%F%P: bfd_hash_table_init failed: %E\n");
  812.     }
  813.  
  814.       if (bfd_hash_lookup (hash, name, false, false) != NULL)
  815.     return true;
  816.  
  817.       if (bfd_hash_lookup (hash, name, true, true) == NULL)
  818.     einfo ("%F%P: bfd_hash_lookup failed: %E\n");
  819.     }
  820.  
  821.   /* We never print more than a reasonable number of errors in a row
  822.      for a single symbol.  */
  823.   if (error_name != (char *) NULL
  824.       && strcmp (name, error_name) == 0)
  825.     ++error_count;
  826.   else
  827.     {
  828.       error_count = 0;
  829.       if (error_name != (char *) NULL)
  830.     free (error_name);
  831.       error_name = buystring (name);
  832.     }
  833.  
  834.   if (error_count < MAX_ERRORS_IN_A_ROW)
  835.     einfo ("%X%C: undefined reference to `%T'\n",
  836.        abfd, section, address, name);
  837.   else if (error_count == MAX_ERRORS_IN_A_ROW)
  838.     einfo ("%D: more undefined references to `%T' follow\n",
  839.        abfd, section, address, name);
  840.  
  841.   return true;
  842. }
  843.  
  844. /* This is called when a reloc overflows.  */
  845.  
  846. /*ARGSUSED*/
  847. static boolean
  848. reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
  849.      struct bfd_link_info *info;
  850.      const char *name;
  851.      const char *reloc_name;
  852.      bfd_vma addend;
  853.      bfd *abfd;
  854.      asection *section;
  855.      bfd_vma address;
  856. {
  857.   if (abfd == (bfd *) NULL)
  858.     einfo ("%P%X: generated");
  859.   else
  860.     einfo ("%X%C:", abfd, section, address);
  861.   einfo (" relocation truncated to fit: %s %T", reloc_name, name);
  862.   if (addend != 0)
  863.     einfo ("+%v", addend);
  864.   einfo ("\n");
  865.   return true;
  866. }
  867.  
  868. /* This is called when a dangerous relocation is made.  */
  869.  
  870. /*ARGSUSED*/
  871. static boolean
  872. reloc_dangerous (info, message, abfd, section, address)
  873.      struct bfd_link_info *info;
  874.      const char *message;
  875.      bfd *abfd;
  876.      asection *section;
  877.      bfd_vma address;
  878. {
  879.   if (abfd == (bfd *) NULL)
  880.     einfo ("%P%X: generated");
  881.   else
  882.     einfo ("%X%C:", abfd, section, address);
  883.   einfo ("dangerous relocation: %s\n", message);
  884.   return true;
  885. }
  886.  
  887. /* This is called when a reloc is being generated attached to a symbol
  888.    that is not being output.  */
  889.  
  890. /*ARGSUSED*/
  891. static boolean
  892. unattached_reloc (info, name, abfd, section, address)
  893.      struct bfd_link_info *info;
  894.      const char *name;
  895.      bfd *abfd;
  896.      asection *section;
  897.      bfd_vma address;
  898. {
  899.   if (abfd == (bfd *) NULL)
  900.     einfo ("%P%X: generated");
  901.   else
  902.     einfo ("%X%C:", abfd, section, address);
  903.   einfo (" reloc refers to symbol `%T' which is not being output\n", name);
  904.   return true;
  905. }
  906.  
  907. /* This is called when a symbol in notice_hash is found.  Symbols are
  908.    put in notice_hash using the -y option.  */
  909.  
  910. /*ARGSUSED*/
  911. static boolean
  912. notice_ysym (info, name, abfd, section, value)
  913.      struct bfd_link_info *info;
  914.      const char *name;
  915.      bfd *abfd;
  916.      asection *section;
  917.      bfd_vma value;
  918. {
  919.   einfo ("%B: %s %s\n", abfd,
  920.      bfd_is_und_section (section) ? "reference to" : "definition of",
  921.      name);
  922.   return true;
  923. }
  924.